package com.xms.configuration.api.controller;


import com.xms.configuration.model.PlatformSetting;
import com.xms.configuration.service.ISettingFactory;
import com.xms.configuration.service.ISettingSaveService;
import com.xms.core.exception.XmsException;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-29
 */
@RestController
@RequestMapping("/{org}/api/settings")
public class SettingsController extends ApiBaseController {
    private ISettingFactory _settingFactory;
    private ISettingSaveService _settingSaveService;
    @Autowired
    public SettingsController(ISettingFactory settingFactory,
                              ISettingSaveService settingSaveService){
        _settingFactory = settingFactory;
        _settingSaveService = settingSaveService;
    }

    @ApiOperation("获取平台参数")
    @GetMapping("platform/get")
    public JsonResultObject getPlatform() throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        return JOk(_settingFactory.getObject(PlatformSetting.class));
    }

    @ApiOperation("保存平台参数")
    @PostMapping("platform/save")
    public JsonResultObject savePlatform(@RequestBody PlatformSetting model) throws XmsException, SQLException, IllegalAccessException {
        _settingSaveService.save(model);
        return JSaveSuccess();
    }
}
